package com.tang.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.tang.auth.application.convert.AuthRoleDTOConverter;
import com.tang.auth.entity.AuthRoleDTO;
import com.tang.auth.domain.entity.AuthRoleBO;
import com.tang.auth.domain.service.AuthRoleDomainService;
import com.tang.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色模块
 *
 * @author Tang
 * @date 2024/2/15 11:07
 **/
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 添加角色
     *
     * @param authRoleDTO 待添加的角色信息
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 11:14
     **/
    @PostMapping("/add")
    public Result<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.RoleController.addRole.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色的key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色的名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoTOBo(authRoleDTO);
            Boolean flag = authRoleDomainService.addRole(authRoleBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.RoleController.addRole.error:{}", JSON.toJSONString(authRoleDTO), e);
            return Result.fail(false);
        }
    }

    /**
     * 更新角色
     *
     * @param authRoleDTO 待更新的信息
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 11:29
     **/
    @PostMapping("/update")
    public Result<Boolean> updateRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.RoleController.updateRole.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色的id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色的名称不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色的key不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoTOBo(authRoleDTO);
            Boolean flag = authRoleDomainService.updateRole(authRoleBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.RoleController.updateRole.error:{}", JSON.toJSONString(authRoleDTO), e);
            return Result.fail(false);
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.RoleController.deleteRole.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色的id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoTOBo(authRoleDTO);
            Boolean flag = authRoleDomainService.deleteRole(authRoleBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.RoleController.deleteRole.error:{}", JSON.toJSONString(authRoleDTO), e);
            return Result.fail(false);
        }
    }
}
